package com.dtx.abstractfactory.tablefactory;

import com.dtx.abstractfactory.factory.Item;
import com.dtx.abstractfactory.factory.Tray;

import java.util.Iterator;

/**具体的零件: TableTray类
 * TableTray类是Tray类的子类,其makeHTML方法处理的是使用<td>和<table>标签输出Item
 */
public class TableTray extends Tray {

    public TableTray(String caption) {
        super(caption);
    }

    @Override
    public String makeHTML() {

        StringBuffer buffer = new StringBuffer();

        buffer.append("<td>");

        buffer.append("<table width=\"100%\" border = \"1\"><tr>");

        buffer.append("<td bgcolor=\"#cccccc\" align=\"center\" colspan=\"" + tray.size() +"\"><b>" +
                caption + "</b></td>");

        buffer.append("</tr>\n");

        buffer.append("<tr>\n");

        Iterator<Object> it = tray.iterator();

        while (it.hasNext()) {
            Item item = (Item) it.next();
            buffer.append(item.makeHTML());
        }

        buffer.append("</tr></table>");

        buffer.append("</td>");

        return buffer.toString();
    }
}
